
#include "../common/spectral_div.h"
#include "../common/itemslist.h"
#include "../common/mem.h"
#include "../common/dynamic_int_arr.h"

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv)
{
	const char* adjacency_mat_file;
	const char* group_file;
	sparse_matrix_arr* original_matrix;
	Vector* Kg;
	elem precision;
	elem dQue = 0;
	dynamic_int_arr* indices;

	int* g1=NULL;
	int* g2=NULL;
	int g1Count = 0;
	int g2Count = 0;

	if (argc != 4)
		nice_exit("Usage: %s <adjacency-mat-file> <group-file> <precision>\n\n", argv[0]);

	adjacency_mat_file = argv[1];
	group_file = argv[2];
	precision = (elem) atof(argv[3]);

	original_matrix = sparse_matrix__read_from_file(adjacency_mat_file, -1);
	indices = dynamic_int_arr__create_from_file(group_file);

	Kg = allocate_Vector(indices->count);

	spectralDiv(original_matrix, precision, indices->values, indices->count,
		&dQue, &g1, &g2, &g1Count, &g2Count, DONT_USE_IMPROVE_DIV);

	printf("%f\n", dQue);

	/*  Print in the correct order */
	if (g1Count == 0)
		printGroup(g2, g2Count);
	else if (g2Count == 0)
		printGroup(g1, g1Count);
	else if (g1[0] < g2[0])
	{
		printGroup(g1, g1Count);
		printGroup(g2, g2Count);
	}
	else
	{
		printGroup(g2, g2Count);
		printGroup(g1, g1Count);
	}

	free_all();

	return 0;
}